rm(list = ls())
library(foreign)
library(wnominate)
library(pscl)
library(readr)
library(psy)
library(psych)
library(nFactors)
library(oc)
library(wnominate)

dataset <- read.csv("roll_call_matrix.csv")


rc.matrix <- dataset[,2:ncol(dataset)]
m <- match(rownames(rc.matrix), rownames(dataset))
mat <- rollcall(rc.matrix, yea = 1, nay = 0)

extra <- read.csv("data_to_append.csv")
names <- extra$Constitution.Name

##############################################################

#Bayesian Ideal Point Model
set.seed(4227494)
idealpoints <- ideal(mat, maxiter = 100000, burnin = 25000, verbose = T, store.item = T)

d1 <- idealpoints$xbar
lower <- as.data.frame(apply(idealpoints$x, 2, function(x) quantile(x, .05, na.rm =T)))
upper <- apply(idealpoints$x, 2, function(x) quantile(x, .95, na.rm =T))

points <- data.frame(extra$Constitution.Name, d1, lower, upper)
names(points) <- c("name", "d1", "lowerd1", "upperd1")
points$name <- as.character(points$name)

m <- match(points$name, extra$Constitution.Name)
table(is.na(m))

points <- merge(points, extra, by.x = "name", by.y = "Constitution.Name",  all.y = T)

points$name[points$name == "Constitution of the United States of America"] <- "USA - 1788"
points$name[points$name == "Constitution of the United States of America + BOR"] <- "USA + BOR - 1791"
points$name[points$name == "Charter: Delaware & Pennsylvania - 1701"] <- "Charter: DE & PA - 1701"

points <- points[order(points$d1),]
write.csv(points, "all_colonies_1d_ideal_points.csv")



##########################################
##########################################

#save item parameters
#Table of discrimination parameters
items <- as.data.frame(idealpoints$betabar)
colnames(items) <- c("Discrimination", "Difficulty")
items$abs.items <- abs(items$Discrimination)
items <- items[rev(order(items$abs.items)),]
write.csv(items, "all_colonies_1d_items.csv")

top.items <- rownames(items)[1:20]
top.items <- as.numeric(gsub("Vote ", "", top.items)) + 1
top.d.items <- names(dataset)[top.items]

hist(items$Discrimination, breaks = 30, main = "Discrimination Parameters", xlab = "Estimated Discrimination Parameter", ylab = "Frequency", freq = T)

##########################################
##########################################

pred.idealpoints <- predict(idealpoints, cutoff=.5)
correct <- pred.idealpoints$overall.percent

#############################################

#2 dimensional model
set.seed(4227492)
idealpoints2 <- ideal(mat, maxiter = 100000, burnin = 75000, verbose = T, store.item = T, d = 2)
d2 <- as.data.frame(idealpoints2$xbar)
lowerd1 <- apply(idealpoints2$x[,,1], 2, function(x) quantile(x, .05, na.rm =T))
upperd1 <- apply(idealpoints2$x[,,1], 2, function(x) quantile(x, .95, na.rm =T))
lowerd2 <- apply(idealpoints2$x[,,2], 2, function(x) quantile(x, .05, na.rm =T))
upperd2 <- apply(idealpoints2$x[,,2], 2, function(x) quantile(x, .95, na.rm =T))
points2 <- data.frame(extra$Constitution.Name, d2, lowerd1, upperd1, lowerd2, upperd2)
names(points2) <- c("name", "d1d2", "d2d2", "lowerd1d2", "upperd1d2", "lowerd2d2", "upperd2d2")

points2$name <- as.character(points2$name)
points2$name[points2$name == "Constitution of the United States of America"] <- "USA - 1788"
points2$name[points2$name == "Constitution of the United States of America + BOR"] <- "USA + BOR - 1791"
points2$name[points2$name == "Charter: Delaware & Pennsylvania - 1701"] <- "Charter: DE & PA - 1701"

write.csv(points2, "all_colonies_2d_ideal_points.csv")

items2 <- as.data.frame(idealpoints2$betabar)
colnames(items2) <- c("Discrimination1", "Discrimination2", "Difficulty")
items2$abs.items12 <- abs(items2$Discrimination1)
items2$abs.items22 <- abs(items2$Discrimination2)
items2 <- items2[order(items2$abs.items12),]

write.csv(items2, "all_colonies_2d_items.csv")


#############################################

#Scale just the states, see if it correlates when charters are omitted. 
rc.matrix2 <- dataset[c(1:15, 27:43),2:ncol(dataset)]
m2 <- match(rownames(rc.matrix2), rownames(dataset))
mat2 <- rollcall(rc.matrix2, yea = 1, nay = 0)

set.seed(4227492)
idealpoints <- ideal(mat2, maxiter = 100000, burnin = 25000, verbose = T, store.item = T)
d1.s <- idealpoints$xbar
lower.s <- as.data.frame(apply(idealpoints$x, 2, function(x) quantile(x, .05, na.rm =T)))
upper.s <- apply(idealpoints$x, 2, function(x) quantile(x, .95, na.rm =T))
points.s <- data.frame(names[c(1:15, 27:43)], d1.s, lower.s, upper.s)
names(points.s) <- c("name", "d1", "lowerd1", "upperd1")

points.s$name <- as.character(points.s$name)
points.s$name[points.s$name == "Constitution of the United States of America"] <- "USA - 1788"
points.s$name[points.s$name == "Constitution of the United States of America + BOR"] <- "USA + BOR - 1791"
points.s$name[points.s$name == "Charter: Delaware & Pennsylvania - 1701"] <- "Charter: DE & PA - 1701"

write.csv(points.s, "just_states_1d_ideal_points.csv")


#############################################

#OPTIMAL CLASSIFICATION
idealpoints <- oc(mat, dims = 1, polarity = c(1))
ocpoints <- as.data.frame(idealpoints$legislators)

ocpoints$name <- names
ocpoints$name[ocpoints$name == "Constitution of the United States of America"] <- "USA - 1788"
ocpoints$name[ocpoints$name == "Constitution of the United States of America + BOR"] <- "USA + BOR - 1791"
ocpoints$name[ocpoints$name == "Charter: Delaware & Pennsylvania - 1701"] <- "Charter: DE & PA - 1701"

write.csv(ocpoints, "all_colonies_1d_ideal_points_OC.csv")


#WNOMINATE METHOD
idealpoints <- wnominate(mat, dims = 1, polarity = 1, lop = 0.05)
points <- as.data.frame(idealpoints$legislators$coord1D)

points$name <- names
points$name <- as.character(points$name)
points$name[points$name == "Constitution of the United States of America"] <- "USA - 1788"
points$name[points$name == "Constitution of the United States of America + BOR"] <- "USA + BOR - 1791"
points$name[points$name == "Charter: Delaware & Pennsylvania - 1701"] <- "Charter: DE & PA - 1701"

write.csv(points, "all_colonies_1d_ideal_points_WNOMINATE.csv")






